local xuandai = fk.CreateSkill{
  name = "plum__xuandai",
}

Fk:loadTranslationTable{
  [xuandai.name] = "悬待",
  [":"..xuandai.name] = "出牌阶段限一次，你可以令一名角色将其一张牌当作【无中生有】对你使用，然后其选择一项："
  .."1.以此法获得的牌在其下次使用牌前不能被使用；"
  .."2.以此法获得的牌在你下次使用牌后需弃置之。",
  ["#plum__xuandai-prompt"] = "悬待：可以令一名角色将其一张牌当作【无中生有】对你使用",

  ["#plum__xuandai-prohibit"] = "以此法获得的牌在你下次使用牌前不能被使用",
  ["#plum__xuandai-discard"] = "以此法获得的牌在其下次使用牌后需弃置之",

  ["@@plum__xuandai_prohibit"] = "悬待[禁]",
  ["@@plum__xuandai_discard"] = "悬待[弃]",

  ["#plum__xuandai-choose"] = "悬待：请选择",
  ["#plum__xuandai-choice"] = "悬待：%from 选择了 %arg",

  ["$plum__xuandai1"] = "受宣王厚遇，引于行役，委以文武，敢不毕力？",
  ["$plum__xuandai2"] = "南北驱驰、载主之忧；九载悬待，当许三生。",
}

---@param player ServerPlayer
---@param target ServerPlayer
---@param card integer
local function addProhibit(player, target, card)
  local mark = player:getTableMark("plum__xuandai_prohibit")
  local id = tostring(target.id)
  mark[id] = mark[id] or {}
  table.insertIfNeed(mark[id], card)
  player.room:setPlayerMark(player, "plum__xuandai_prohibit", mark)
end

xuandai:addEffect("active", {
  anim_type = "drawcard",
  prompt = "#plum__xuandai-prompt",
  can_use = function (self, player)
    return player:usedSkillTimes(xuandai.name, Player.HistoryRound) == 0
  end,
  max_phase_use_time = 1,
  card_num = 0,
  target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected, selected_cards)
    if #selected == 0 then
      local card = Fk:cloneCard("ex_nihilo")
      card.skillName = xuandai.name
      return not to_select:isProhibited(player, card)
    end
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local cards = table.filter(target:getCardIds("he"), function (id)
      local card = Fk:cloneCard("ex_nihilo")
      card.skillName = xuandai.name
      card:addSubcard(id)
      return not (target:prohibitUse(Fk:getCardById(id)) or target:isProhibited(player, card))
    end)
    if #cards == 0 then return end

    local card = room:askToCards(target, {
      skill_name = xuandai.name,
      min_num = 1,
      max_num = 1,
      cancelable = false,
      include_equip = true,
      pattern = ".|.|.|.|.|.|"..table.concat(cards, ",")
    })

    local _card = Fk:cloneCard("ex_nihilo")
    _card.skillName = xuandai.name
    _card:addSubcards(card)

    room:useCard{
      card = _card,
      from = target,
      tos = {player},
    }

    if target:isAlive() and player:isAlive() then
      local choice = room:askToChoice(target, {
        choices = { "#plum__xuandai-prohibit", "#plum__xuandai-discard" },
        cancelable = false,
        skill_name = xuandai.name,
        prompt = "#plum__xuandai-choose"
      })
      room:sendLog{type = "#plum__xuandai-choice", from = target.id, arg = choice, toast = true}

      local drawed_cards = player:getTableMark(xuandai.name) ---@type integer[]
      room:setPlayerMark(player, xuandai.name, 0)
      if #drawed_cards == 0 then return end
      if choice == "#plum__xuandai-prohibit" then
        for _, id in ipairs(drawed_cards) do
          room:addCardMark(Fk:getCardById(id), "@@plum__xuandai_prohibit", 1)
          addProhibit(player, target, id)
        end
      else
        for _, id in ipairs(drawed_cards) do
          room:addCardMark(Fk:getCardById(id), "@@plum__xuandai_discard", 1)
          room:addTableMarkIfNeed(player, "plum__xuandai_discard", id)
        end
      end
    end
  end
})

xuandai:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    if player:hasSkill(xuandai.name, true) and card:getMark("@@plum__xuandai_prohibit") > 0 then
      return true
    end
  end
})

xuandai:addEffect(fk.CardUsing, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(xuandai.name) and player == target and table.find(player:getCardIds("h"), function (id)
      return Fk:getCardById(id):getMark("@@plum__xuandai_discard") > 0 and not player:prohibitDiscard(id)
    end)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local cards = table.filter(player:getCardIds("h"), function (id)
      return Fk:getCardById(id):getMark("@@plum__xuandai_discard") > 0 and not player:prohibitDiscard(id)
    end)
    for _, id in ipairs(cards) do
      player.room:setCardMark(Fk:getCardById(id), "@@plum__xuandai_discard", 0)
    end
    player.room:throwCard(cards, xuandai.name, player, player)
  end,
})

xuandai:addEffect(fk.CardUsing, {
  mute = true,
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(xuandai.name) then
      local cards = player:getTableMark("plum__xuandai_prohibit")[tostring(target.id)] or {}
      if #cards > 0 then
        return true
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local cards = player:getTableMark("plum__xuandai_prohibit")[tostring(target.id)] or {} ---@type integer[]
    for _, id in ipairs(cards) do
      player.room:removeCardMark(Fk:getCardById(id), "@@plum__xuandai_prohibit", 1)
    end
  end
})

xuandai:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(xuandai.name) then
      for _, move in ipairs(data) do
        if move.to and move.to == player and move.moveReason == fk.ReasonDraw then
          local use = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
          if use then
            return use.data.card.skillName == xuandai.name
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local cards = {}
    for _, move in ipairs(data) do
      if move.to and move.to == player then
        for _, info in ipairs(move.moveInfo) do
          table.insert(cards, info.cardId)
        end
      end
    end
    player.room:setPlayerMark(player, xuandai.name, cards)
  end
})

return xuandai